package com.cy.utils;

import com.cy.config.EnumConst;
import com.cy.sdk.sms.SMSValidCode;
import com.jfinal.plugin.ehcache.CacheKit;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * Created by Administrator on 14-1-26.
 * app 发送手机短信验证码
 */
public class ValidCodeUtil {

    /***
     * app端 短信验证码校验
     * @param account
     * @param regCode
     * @return
     */
    public static Integer checkValidCode(String account,String regCode) {
        Integer code = 0;
        SMSValidCode smsValidCode = CacheKit.get(SMSValidCode.CACHE_CODE, account);
        if (smsValidCode == null || StringUtils.isBlank(account) || StringUtils.isBlank(regCode) || !smsValidCode.valid(regCode)) {
        //if (StringUtils.isBlank(account) || StringUtils.isBlank(regCode)) {
            code = EnumConst.RetCode.SMSCODE_FAILD.getCode();
        }else {
            code= EnumConst.RetCode.SUCCESS.getCode();
        }
        return  code;
    }

    /***
     * 创建 发送手机短信验证码
     * @param account
     * @return
     */
    public static Integer sendValidCode(String account) {
        Integer code = 0;
        Pattern p = Pattern.compile("^(1([3-9])([0-9]))\\d{8}$");
        if (StringUtils.isBlank(account) || account.length() < 11 || !p.matcher(account).matches()) {
            code =-1;
            return code;
        }

        SMSValidCode smsValidCode = CacheKit.get(SMSValidCode.CACHE_CODE, account);

        String validCode;
        if (smsValidCode == null || smsValidCode.nextSend()) {
//            if (){
                validCode = SMSKit.sendRandomCheckCode(account,"");
                if (smsValidCode == null) {
                    smsValidCode = new SMSValidCode();
                }
                smsValidCode.setMobile(account);
                smsValidCode.setValidCode(validCode);
                smsValidCode.setSendTime(System.currentTimeMillis());
                CacheKit.put(SMSValidCode.CACHE_CODE, account, smsValidCode);
                code = 1;
          /*  }else{
                code = 2;
            }*/

        } else {
            code = -1;
        }
        return code;
    }

    /***
     * 创建 发送手机短信验证码
     * @param account
     * @return
     */
    public static Integer sendValidCode(String account, String accountName) {
        Integer code = 0;
        Pattern p = Pattern.compile("^(1([3-9])([0-9]))\\d{8}$");
        if (StringUtils.isBlank(account) || account.length() < 11 || !p.matcher(account).matches()) {
            code =-1;
        }

        SMSValidCode smsValidCode = CacheKit.get(SMSValidCode.CACHE_CODE, account);

        String validCode;
        if (smsValidCode == null || smsValidCode.nextSend()) {
            validCode = SMSKit.sendRandomCheckCode(account,accountName);
            if (smsValidCode == null) {
                smsValidCode = new SMSValidCode();
            }
            smsValidCode.setMobile(account);
            smsValidCode.setValidCode(validCode);
            smsValidCode.setSendTime(System.currentTimeMillis());
            CacheKit.put(SMSValidCode.CACHE_CODE, account, smsValidCode);
            code = 1;
        } else {
            code = -1;
        }
        return code;
    }

}
